package com.zliblike_platform.controller;

import com.zliblike_platform.dto.BookRequestDTO;
import com.zliblike_platform.dto.BookRequestUpdateDTO;
import com.zliblike_platform.service.BookRequestService;
import com.zliblike_platform.vo.BookRequestVO;
import com.zliblike_platform.vo.PageResult;
import com.zliblike_platform.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * 书籍请求控制器
 */
@RestController
@RequestMapping("/api/book-requests")
public class BookRequestController {

    @Autowired
    private BookRequestService bookRequestService;
    
    /**
     * 创建书籍请求
     */
    @PostMapping
    public ResponseResult<Long> createRequest(@RequestBody BookRequestDTO requestDTO) {
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = Long.parseLong(authentication.getName());
        
        // 创建请求
        Long requestId = bookRequestService.createRequest(userId, requestDTO);
        
        if (requestId != null) {
            return ResponseResult.success("请求创建成功", requestId);
        } else {
            return ResponseResult.fail(4001, "请求创建失败，ISBN可能已存在");
        }
    }
    
    /**
     * 更新书籍请求状态
     */
    @PostMapping("/status")
    public ResponseResult<Boolean> updateRequestStatus(@RequestBody BookRequestUpdateDTO updateDTO) {
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = Long.parseLong(authentication.getName());
        
        // 更新状态
        boolean success = bookRequestService.updateRequestStatus(userId, updateDTO);
        
        if (success) {
            return ResponseResult.success("状态更新成功", true);
        } else {
            return ResponseResult.fail(4002, "状态更新失败");
        }
    }
    
    /**
     * 获取所有书籍请求（分页）
     */
    @GetMapping
    public ResponseResult<PageResult<BookRequestVO>> getAllRequests(
            @RequestParam(required = false) String status,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size) {
        
        // 查询请求
        PageResult<BookRequestVO> result = bookRequestService.getAllRequests(status, page, size);
        
        return ResponseResult.success(result);
    }
    
    /**
     * 获取当前用户的书籍请求（分页）
     */
    @GetMapping("/my")
    public ResponseResult<PageResult<BookRequestVO>> getMyRequests(
            @RequestParam(required = false) String status,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size) {
        
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = Long.parseLong(authentication.getName());
        
        // 查询请求
        PageResult<BookRequestVO> result = bookRequestService.getUserRequests(userId, status, page, size);
        
        return ResponseResult.success(result);
    }
    
    /**
     * 获取书籍请求详情
     */
    @GetMapping("/{requestId}")
    public ResponseResult<BookRequestVO> getRequestDetail(@PathVariable Long requestId) {
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = Long.parseLong(authentication.getName());
        
        // 查询详情
        BookRequestVO request = bookRequestService.getRequestDetail(requestId, userId);
        
        if (request != null) {
            return ResponseResult.success(request);
        } else {
            return ResponseResult.fail(4003, "请求不存在");
        }
    }
    
    /**
     * 删除书籍请求
     */
    @DeleteMapping("/{requestId}")
    public ResponseResult<Boolean> deleteRequest(@PathVariable Long requestId) {
        // 获取当前用户ID
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = Long.parseLong(authentication.getName());
        
        // 删除请求
        boolean success = bookRequestService.deleteRequest(requestId, userId);
        
        if (success) {
            return ResponseResult.success("请求删除成功", true);
        } else {
            return ResponseResult.fail(4004, "请求删除失败，可能不存在或无权限删除");
        }
    }
    
    /**
     * 检查ISBN是否已存在
     */
    @GetMapping("/check-isbn")
    public ResponseResult<Boolean> checkIsbn(@RequestParam String isbn) {
        boolean exists = bookRequestService.isbnExists(isbn);
        return ResponseResult.success(exists);
    }
} 